<%@ page contentType="text/html;charset=UTF-8"%>
<%@page import="java.util.Date"%>
<%@ include file="/common/taglibs.jsp"%>
<%@ page import="org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter"%>
<%@ page import="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"%>
<%@ page import="org.springframework.security.core.AuthenticationException"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Mini-Web 登录页</title>
		<%@ include file="/common/meta.jsp"%>
		<link href="${staticCtx}/style/yui.css" type="text/css" rel="stylesheet" />
		<link href="${staticCtx}/style/style.css" type="text/css" rel="stylesheet" />
		<script src="${staticCtx}/script/jquery/jquery.js" type="text/javascript"></script>
		<script src="${staticCtx}/script/jquery/validate/jquery.validate.js" type="text/javascript"></script>
		<script src="${staticCtx}/script/jquery/validate/messages_cn.js" type="text/javascript"></script>
		<script type="text/javascript">
		$(document).ready(function() {
			$("#loginForm").validate();
		});
		
		function refreshCaptcha() {
				$('#captchaImg').hide().attr('src','${ctx}/security/jcaptcha.jpg?' + Math.floor(Math.random()*100)).fadeIn();
			}
	</script>
	</head>
	<body>
		<div id="doc3">
			<%@ include file="/common/header.jsp"%>
			<div id="bd">
				<div id="yui-main">
					<div class="yui-b">
						<%
						    if ("1".equals(request.getParameter("error"))) {
						%>
						<div class="error">
							用户名密码错误,请重试.
						</div>
						<%
						    }
						    if ("2".equals(request.getParameter("error"))) {
						%>
						<div class="error">
							验证码错误,请重试.
						</div>
						<%
						    }
						    if ("3".equals(request.getParameter("error"))) {
						%>
						<div class="error">
							此帐号已从别处登录.
						</div>
						<%
						    }
						%>

						<%
						    if (session
						            .getAttribute(AbstractAuthenticationProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY) != null) {
						%>
						<div class="error">
							登录失败，请重试.
						</div>
						<%
						    }
						%>
						<form id="loginForm" action="${ctx}/j_spring_security_check" method="post" style="margin-top: 1em">
							<table class="noborder">
								<tr>
									<td>
										<label>
											用户名:
										</label>
									</td>
									<td>
										<input type='text' id='j_username' name='j_username' class="required" />
									</td>
									<td rowspan="3">
										<img id="captchaImg" src="${ctx}/security/jcaptcha.jpg" />
									</td>
								</tr>
								<tr>
									<td>
										密码:
									</td>
									<td>
										<input type='password' name='j_password' class="required" />
									</td>
								</tr>
								<tr>
									<td>
										验证码:
									</td>
									<td>
										<input type='text' name='j_captcha' class="required" />
									</td>
								</tr>
								<tr>
									<td colspan='3'>
										<input type="checkbox" name="_spring_security_remember_me" />
										两周内记住我
										<span style="margin-left: 25px"><a href="javascript:refreshCaptcha()">看不清楚换一张</a> </span>
									</td>
								</tr>
								<tr>
									<td colspan='1'>
										<input value="登录" type="submit" />
									</td>
									<td colspan='1'>
										<a id="loginByJson" href="${ctx}/account/user/login">登录(json)</a>
									</td>
									<td colspan='1'>
										<a id="loginByXml" href="${ctx}/account/user/login">登录(xml)</a>
									</td>
								</tr>
							</table>
						</form>
						<div>
							(管理员
							<b>admin/admin</b>, 普通用户
							<b>user/user</b>)
						</div>
					</div>
				</div>
			</div>
			<%@ include file="/common/footer.jsp"%>
		</div>
	</body>

	<script type="text/javascript"> 
		$("#loginByJson").click(function() {
	      $.ajax({
	            type     : 'POST',
	            url      : this.href,
	            dataType : 'json',
	            data     : {
	              username : 'admin',
	              password : 'e10adc3949ba59abbe56e057f20f883e'
	            },
	            success  : function(json) {
	              console.info('当前用户: '+ json.username);
	              console.log('权限: ' + json.roleNames);
	              console.log('姓: ' + json.lastName);
	              console.log('名: ' + json.firstName);
	              console.log('电邮: ' + json.email);
	            },
	            error    : function(xhr) {
	              console.error('error.');
	            }
	          });
	
	      return false;
	    });
	
	$("#loginByXml").click(function() {
	      $.ajax({
	            type       : 'POST',
	            url        : this.href,
	            data       : {
	              username : 'admin',
	              password : 'e10adc3949ba59abbe56e057f20f883e'
	            },
	            beforeSend : function(req) {
	              req.setRequestHeader("Accept", "application/application+xml");
	            },
	            success    : function(xml) {
	              console.info('当前用户: ' + $(xml).find('username').text());
	              console.log('权限: ' + $(xml).find('roleNames').text());
	              console.log('姓: ' + $(xml).find('lastName').text());
	              console.log('名: ' + $(xml).find('firstName').text());
	              console.log('电邮: ' + $(xml).find('email').text());
	            },
	            error      : function(xhr) {
	              console.error('error.');
	            }
	          });
	
	      return false;
	    });
	</script>

</html>

